namespace = cr_lust

###############################################
# CARNAL REALMS RELIGION LUST METER
#
# religion_lust intentionally keeps the legacy
# variable name so CM saves retain their progress
# when Carnal Realms replaces Christianity Mod.
###############################################

# Queue initialization one day after startup. The Modding Bible notes that
# parts of the game state are not fully available during on_startup.
character_event = {
	id = cr_lust.0
	hide_window = yes
	is_triggered_only = yes
	only_playable = yes

	trigger = {
		ai = no
		NOT = { has_global_flag = cr_lust_meter_initialized }
	}

	immediate = {
		character_event = { id = cr_lust.1 days = 1 }
	}
}

# Initialize every religion active in CR. Existing values at or above 1 are
# preserved, including values inherited from a Christianity Mod save.
character_event = {
	id = cr_lust.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOT = { has_global_flag = cr_lust_meter_initialized }
	}

	immediate = {
		catholic = { cr_initialize_religion_lust_effect = yes }
		fraticelli = { cr_initialize_religion_lust_effect = yes }
		lollard = { cr_initialize_religion_lust_effect = yes }
		orthodox = { cr_initialize_religion_lust_effect = yes }
		miaphysite = { cr_initialize_religion_lust_effect = yes }
		iconoclast = { cr_initialize_religion_lust_effect = yes }
		paulician = { cr_initialize_religion_lust_effect = yes }
		nestorian = { cr_initialize_religion_lust_effect = yes }
		messalian = { cr_initialize_religion_lust_effect = yes }

		sunni = { cr_initialize_religion_lust_effect = yes }
		yazidi = { cr_initialize_religion_lust_effect = yes }
		ibadi = { cr_initialize_religion_lust_effect = yes }
		shiite = { cr_initialize_religion_lust_effect = yes }
		druze = { cr_initialize_religion_lust_effect = yes }
		qarmatian = { cr_initialize_religion_lust_effect = yes }

		pagan = { cr_initialize_religion_lust_effect = yes }
		norse_pagan = { cr_initialize_religion_lust_effect = yes }
		tengri_pagan = { cr_initialize_religion_lust_effect = yes }
		baltic_pagan = { cr_initialize_religion_lust_effect = yes }
		finnish_pagan = { cr_initialize_religion_lust_effect = yes }
		aztec = { cr_initialize_religion_lust_effect = yes }
		slavic_pagan = { cr_initialize_religion_lust_effect = yes }
		west_african_pagan = { cr_initialize_religion_lust_effect = yes }
		zun_pagan = { cr_initialize_religion_lust_effect = yes }
		hellenic_pagan = { cr_initialize_religion_lust_effect = yes }
		bon = { cr_initialize_religion_lust_effect = yes }

		zoroastrian = { cr_initialize_religion_lust_effect = yes }
		mazdaki = { cr_initialize_religion_lust_effect = yes }
		manichean = { cr_initialize_religion_lust_effect = yes }
		khurmazta = { cr_initialize_religion_lust_effect = yes }

		jewish = { cr_initialize_religion_lust_effect = yes }
		samaritan = { cr_initialize_religion_lust_effect = yes }
		karaite = { cr_initialize_religion_lust_effect = yes }
		canaanite_mesopotamian = { cr_initialize_religion_lust_effect = yes }

		hindu = { cr_initialize_religion_lust_effect = yes }
		buddhist = { cr_initialize_religion_lust_effect = yes }
		jain = { cr_initialize_religion_lust_effect = yes }
		taoist = { cr_initialize_religion_lust_effect = yes }

		set_global_flag = cr_lust_meter_initialized
	}
}

# Refresh a landed character's religious influence. Realm size is divided by
# ten; a religious head receives the same additional 100 influence used by CM.
character_event = {
	id = cr_lust.2
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes

	immediate = {
		clr_character_flag = cr_lust_startup_refresh_queued
		set_variable = { which = cr_lust_influence_multiplier value = 0 }
		export_to_variable = {
			which = cr_lust_influence_multiplier
			value = realm_size
		}
		divide_variable = { which = cr_lust_influence_multiplier value = 10 }
		if = {
			limit = { controls_religion = yes }
			change_variable = { which = cr_lust_influence_multiplier value = 100 }
		}
	}
}

# Public entry point for events and scripted effects. Set lust_increase before
# calling this event; values below 1 default to 1. Unlanded characters have no
# realm influence and therefore cannot move the religion-wide meter.
character_event = {
	id = cr_lust.3
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_variable = { which = cr_lust_influence_multiplier value = 0 }
		if = {
			limit = { is_ruler = yes }
			export_to_variable = {
				which = cr_lust_influence_multiplier
				value = realm_size
			}
			divide_variable = { which = cr_lust_influence_multiplier value = 10 }
			if = {
				limit = { controls_religion = yes }
				change_variable = { which = cr_lust_influence_multiplier value = 100 }
			}
		}

		if = {
			limit = {
				NOT = { check_variable = { which = lust_increase value = 1 } }
			}
			set_variable = { which = lust_increase value = 1 }
		}

		multiply_variable = {
			which = lust_increase
			which = cr_lust_influence_multiplier
		}
		religion_scope = {
			cr_initialize_religion_lust_effect = yes
			change_variable = {
				which = religion_lust
				which = lust_increase
				who = ROOT
			}
		}
		set_variable = { which = lust_increase value = 0 }
	}
}

# Delay startup influence calculation by one day for the same load-order reason
# as the religion-variable bootstrap.
character_event = {
	id = cr_lust.4
	hide_window = yes
	is_triggered_only = yes
	only_playable = yes

	trigger = {
		ai = no
		is_ruler = yes
		NOT = { has_character_flag = cr_lust_startup_refresh_queued }
	}

	immediate = {
		set_character_flag = cr_lust_startup_refresh_queued
		character_event = { id = cr_lust.2 days = 1 }
	}
}
